// 爬取豆瓣电影 TOP250
package main

import (
	"fmt"
	"log"
	"strings"
	"time"
	"unicode/utf8"

	"github.com/PuerkitoBio/goquery"

	"gitee.com/rickiel/douban-movie/model"
	"gitee.com/rickiel/douban-movie/parse"
)

var (
	BaseUrl = "https://movie.douban.com/top250"
)

// 新增数据
func Add(movies []parse.DoubanMovie) {
	for index, movie := range movies {
		if err := model.DB.Create(&movie).Error; err != nil {
			log.Printf("db.Create index: %s, err : %v", index, err)
		}
	}
}

// 开始爬取
func Start() {
	var movies []parse.DoubanMovie

	pages := parse.GetPages(BaseUrl)
	for _, page := range pages {
		resp, err := parse.NewDocument(strings.Join([]string{BaseUrl, page.Url}, ""))
		doc, err := goquery.NewDocumentFromResponse(resp)
		if err != nil {
			log.Println(err)
		}

		movies = append(movies, parse.ParseMovies(doc)...)

		time.Sleep(time.Duration(10) * time.Second)
	}

	Add(movies)
}

func main() {
	// Start()

	// url := "https://movie.douban.com/top250"
	// resp, err := http.Get(url)
	// if err != nil {
	// 	panic(err)
	// }
	// if resp.StatusCode != 200 {
	// 	fmt.Println(resp.StatusCode)
	// }
	// doc, err := goquery.NewDocumentFromReader(resp.Body)
	// doc.Find(".content").Each(func(i int, s *goquery.Selection) {
	// 	fmt.Println(s.Text())
	// })
	a := "廖永福aa"
	//b := 'c'

	fmt.Printf("a: %T, %v, len: %v", a, a, utf8.RuneCountInString(a))
	//fmt.Printf("b: %T, %v, len: %v", b, b, utf8.RuneCountInString(b))

	//defer model.DB.Close()
}
